package mr.ali.webvideo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mr.ali.webvideo.Exception.ServiceException;
import mr.ali.webvideo.model.CollectionPlay;
import mr.ali.webvideo.mapper.CollectionPlayMapper;
import mr.ali.webvideo.model.CollectionPlayTag;
import mr.ali.webvideo.service.ICollectionPlayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mr.ali.webvideo.service.ICollectionPlayTagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-16
 */
@Service
public class CollectionPlayServiceImpl extends ServiceImpl<CollectionPlayMapper, CollectionPlay> implements ICollectionPlayService {
    @Resource
    private CollectionPlayMapper collectionPlayMapper;

    @Resource
    private ICollectionPlayTagService collectionPlayTagService;

    /**
     * ID查询
     */
    @Override
    public List<CollectionPlay> videoTagsById(Integer id) {
        QueryWrapper<CollectionPlay> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("displayed", 1);
        // 根据查询条件获取视频列表
        List<CollectionPlay> videoList = collectionPlayMapper.selectList(wrapper);
        for (CollectionPlay video : videoList) {
            CollectionPlayTag tag = collectionPlayTagService.collectionPlayTagById(video.getTags());
            video.setTagsName(tag.getName());
        }
        return videoList;
    }

    /**
     * 随机获取
     */
    @Override
    public PageInfo<CollectionPlay> findAllByOrderLastPlay(Integer pageNum, Integer pageSize, Integer tags) {

        // 判断翻页参数是否为空
        if (pageNum == null || pageSize == null) {
            // 抛出翻页参数错误异常
            throw ServiceException.GONE("翻页参数错误");
        }
        // 创建查询条件包装器
        LambdaQueryWrapper<CollectionPlay> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectionPlay::getDisplayed, 1);
        if (tags != null && tags != 0) {
            wrapper.eq(CollectionPlay::getTags, tags);
        }
        wrapper.last("ORDER BY RAND()");
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        // 根据查询条件获取视频列表
        List<CollectionPlay> videoList = collectionPlayMapper.selectList(wrapper);

        // Java 层去重
        List<CollectionPlay> uniqueTags = videoList.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(
                                CollectionPlay::getId,
                                item -> item,
                                (existing, replacement) -> existing
                        ),
                        map -> new ArrayList<>(map.values())
                ));

        // ✅ 使用 PageHelper 自动封装的 PageInfo（不是你自己 new 的！）

        for (CollectionPlay video : uniqueTags) {
            CollectionPlayTag collectionPlay = collectionPlayTagService.collectionPlayTagById(video.getTags());
            video.setTagsName(collectionPlay.getName());
        }
        PageInfo<CollectionPlay> pageInfo = new PageInfo<>(videoList);
        pageInfo.setList(uniqueTags);
        return pageInfo;
    }



    /**
     * 标题来模糊查询
     */
    @Override
    public PageInfo<CollectionPlay> searchCollections(String name, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<CollectionPlay> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(CollectionPlay::getName, name);
        }
        wrapper.eq(CollectionPlay::getDisplayed, 1);
        wrapper.orderByDesc(CollectionPlay::getGmtCreate);
        PageHelper.startPage(pageNum, pageSize);
        List<CollectionPlay> list = collectionPlayMapper.selectList(wrapper);
        for (CollectionPlay video : list) {
            CollectionPlayTag collectionPlay = collectionPlayTagService.collectionPlayTagById(video.getTags());
            video.setTagsName(collectionPlay.getName());
        }
        return new PageInfo<>(list);
    }
}
